#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import numpy as np
import pandas as pd
import torch
import numpy as np



tdata = pd.read_csv('course_1w.csv')
train_data = tdata.copy()
# 把用户id和项目id的类别换成枚举类
train_data = train_data.astype({'user_id': 'category', 'course_id': 'category'})

# 实现了绝对id到相对id的映射
train_user_ids = torch.LongTensor(train_data['user_id'].cat.codes.values)
train_item_ids = torch.LongTensor(train_data['course_id'].cat.codes.values)
new_user_id = train_user_ids.tolist()
new_item_id = train_item_ids.tolist()


user_dict = dict(zip(train_data['user_id'].cat.codes.values.tolist(),train_data['user_id'].values.tolist()))
item_dict = dict(zip(train_data['course_id'].cat.codes.values.tolist(),train_data['course_id'].values.tolist()))
# print(user_dict)
# print(item_dict)
np.save('user_dict.npy', user_dict)
np.save('item_dict.npy', item_dict)

train_data['user_id'] = new_user_id
train_data['course_id'] = new_item_id
train_data.drop(['update_time','dislike_status','classify_name'],axis=1,inplace=True)
train_data.to_csv('fslData.csv',index=None)

all_inter = pd.DataFrame()
all_inter['user_id'] = train_data['user_id']
all_inter['course_id'] = train_data['course_id']
all_inter.to_csv('all_inter.csv',index=None)

only_collect = pd.DataFrame()
only_collect['user_id'] = train_data['user_id']
only_collect['course_id'] = train_data['course_id']
collect_list = train_data['collect_status'].values.tolist()
collect_list = [0 if i=='NO' else 1 for i in collect_list]
only_collect['collect_status'] = collect_list
only_collect.to_csv('only_collect.csv',index=None)

only_commit = pd.DataFrame()
only_commit['user_id'] = train_data['user_id']
only_commit['course_id'] = train_data['course_id']
commit_list = train_data['commit_status'].values.tolist()
commit_list = [0 if i=='NO' else 1 for i in commit_list]
only_commit['commit_status'] = commit_list
only_commit.to_csv('only_commit.csv',index=None)

only_score = pd.DataFrame()
only_score['user_id'] = train_data['user_id']
only_score['course_id'] = train_data['course_id']
score_list = train_data['score'].values.tolist()
score_list = [i/20 for i in score_list]
only_score['score'] = score_list
only_score.to_csv('only_score.csv',index=None)

only_duration = pd.DataFrame()
only_duration['user_id'] = train_data['user_id']
only_duration['course_id'] = train_data['course_id']
duration_list = train_data['duration'].values.tolist()
duration_list = [0 if i<10 else 1 for i in duration_list]
only_duration['duration'] = duration_list
only_duration.to_csv('only_duration.csv',index=None)



onehot = pd.read_csv('course_1w_vector2.csv')
onehot.drop(['classify_name'],axis=1,inplace=True)
onehot['course_id'] = onehot['course_id'].map(item_dict)
print(onehot)
onehot.to_csv('onehotdata.csv',index=None)